package org.example.managere.service.feibiao.impl;

import lombok.RequiredArgsConstructor;
import org.example.managere.dto.feibiao.AmArchivesBaseDto;
import org.example.managere.entity.feibiao.AmArchivesBase;
import org.example.managere.repository.feibiao.AmArchivesBaseRepository;
import org.example.managere.service.feibiao.AmArchivesBaseService;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 电机基本信息服务实现类
 */
@Service
@RequiredArgsConstructor
public class AmArchivesBaseServiceImpl implements AmArchivesBaseService {
    
    private final AmArchivesBaseRepository amArchivesBaseRepository;
    private final ModelMapper modelMapper;
    
    /**
     * 将实体转换为DTO
     */
    private AmArchivesBaseDto convertToDto(AmArchivesBase amArchivesBase) {
        return modelMapper.map(amArchivesBase, AmArchivesBaseDto.class);
    }
    
    /**
     * 将DTO转换为实体
     */
    private AmArchivesBase convertToEntity(AmArchivesBaseDto amArchivesBaseDto) {
        return modelMapper.map(amArchivesBaseDto, AmArchivesBase.class);
    }
    
    @Override
    @Transactional
    public AmArchivesBaseDto save(AmArchivesBaseDto amArchivesBaseDto) {
        AmArchivesBase amArchivesBase = convertToEntity(amArchivesBaseDto);
        
        // 设置创建和更新时间
        Date now = new Date();
        amArchivesBase.setCreatedAt(now);
        amArchivesBase.setUpdatedAt(now);
        
        AmArchivesBase savedEntity = amArchivesBaseRepository.save(amArchivesBase);
        return convertToDto(savedEntity);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Optional<AmArchivesBaseDto> findById(String motorId) {
        return amArchivesBaseRepository.findById(motorId)
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional
    public AmArchivesBaseDto update(AmArchivesBaseDto amArchivesBaseDto) {
        Optional<AmArchivesBase> existingEntityOpt = amArchivesBaseRepository.findById(amArchivesBaseDto.getMotorId());
        
        if (existingEntityOpt.isPresent()) {
            AmArchivesBase existingEntity = existingEntityOpt.get();
            
            // 保留创建时间
            Date createdAt = existingEntity.getCreatedAt();
            
            // 更新实体
            AmArchivesBase updatedEntity = convertToEntity(amArchivesBaseDto);
            updatedEntity.setCreatedAt(createdAt);
            updatedEntity.setUpdatedAt(new Date());
            
            AmArchivesBase savedEntity = amArchivesBaseRepository.save(updatedEntity);
            return convertToDto(savedEntity);
        } else {
            // 如果不存在，创建新实体
            return save(amArchivesBaseDto);
        }
    }
    
    @Override
    @Transactional
    public void deleteById(String motorId) {
        amArchivesBaseRepository.deleteById(motorId);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmArchivesBaseDto> findAll() {
        return amArchivesBaseRepository.findAll().stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<AmArchivesBaseDto> findAll(Pageable pageable) {
        return amArchivesBaseRepository.findAll(pageable)
                .map(this::convertToDto);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmArchivesBaseDto> searchByKeyword(String keyword) {
        return amArchivesBaseRepository.searchByKeyword(keyword).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmArchivesBaseDto> findByMotorType(String motorType) {
        return amArchivesBaseRepository.findByMotorTypeContaining(motorType).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<AmArchivesBaseDto> findByManufacturer(String manufacturer) {
        return amArchivesBaseRepository.findByManufacturerContaining(manufacturer).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<AmArchivesBaseDto> searchByKeywordPaged(String keyword, Pageable pageable) {
        return amArchivesBaseRepository.searchByKeywordPaged(keyword, pageable)
                .map(this::convertToDto);
    }
} 